@inherits MudComponentBase
@inject IStringLocalizer<Users> L
<MudDialog>
    <DialogContent>
        <MudForm Model="@model" @ref="@form" Validation="@(modelValidator.ValidateValue)" >
            <MudGrid>
                <MudItem xs="12">
                     <MudTextField Label="@L["User Name"]" For="@(()=>model.UserName)" Value="model.UserName" ></MudTextField>
                </MudItem>
                <MudItem xs="12" >
                <MudTextField 
                          Immediate="true"
                          Label="@L["New Password"]"
                          @bind-Value="model.Password"
                          For="@(() => model.Password)"
                          Variant="Variant.Text"
                          InputType="@PasswordInput"
                          Adornment="Adornment.End"
                          AdornmentIcon="@PasswordInputIcon"
                          Required="true"
                          RequiredError="@L["password is required!"]"
                          OnAdornmentClick="TogglePasswordVisibility" Class="mt-4" />
        </MudItem>
        <MudItem xs="12" >
            <MudTextField 
                          Immediate="true"
                          Label="@L["Confirm New Password"]"
                          @bind-Value="model.ConfirmPassword"
                          For="@(() => model.ConfirmPassword)"
                          Variant="Variant.Text"
                          InputType="@PasswordInput"
                          Adornment="Adornment.End"
                          AdornmentIcon="@PasswordInputIcon"
                          Required="true"
                          RequiredError="@L["password is required!"]"
                          OnAdornmentClick="TogglePasswordVisibility" Class="mt-4" />
        </MudItem>
            </MudGrid>
        </MudForm>
    </DialogContent>
    <DialogActions>
        <MudButton OnClick="Cancel">@L["Cancel"]</MudButton>
        <MudButton Color="Color.Primary" OnClick="Submit">@L["Ok"]</MudButton>
    </DialogActions>
</MudDialog>
@code {
    MudForm form = default!;
    ResetPasswordFormModelValidator modelValidator = new ResetPasswordFormModelValidator();
    [EditorRequired] [Parameter] public ResetPasswordFormModel model { get; set; } = default!;
    [CascadingParameter]
    MudDialogInstance MudDialog { get; set; } = default!;
    bool PasswordVisibility;
    InputType PasswordInput = InputType.Password;
    string PasswordInputIcon = Icons.Material.Filled.VisibilityOff;
    void TogglePasswordVisibility()
    {
        @if (PasswordVisibility)
        {
            PasswordVisibility = false;
            PasswordInputIcon = Icons.Material.Filled.VisibilityOff;
            PasswordInput = InputType.Password;
        }
        else
        {
            PasswordVisibility = true;
            PasswordInputIcon = Icons.Material.Filled.Visibility;
            PasswordInput = InputType.Text;
        }
    }
    async Task Submit() {
        await  form.Validate();
        if (form.IsValid)
        {
            MudDialog.Close(DialogResult.Ok(true));
        }

    }
    void Cancel() => MudDialog.Cancel();
}
